
package com.ruoyi.pay.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.oss.core.OssClient;
import com.ruoyi.oss.enumd.AccessPolicyType;
import com.ruoyi.oss.factory.OssFactory;
import com.ruoyi.pay.consts.CS;
import com.ruoyi.pay.domain.PayInterfaceDefine;
import com.ruoyi.pay.domain.SysOssPay;
import com.ruoyi.pay.mapper.SysOssPayMapper;
import com.ruoyi.pay.service.impl.PayInterfaceConfigService;
import com.ruoyi.pay.vo.ApiRes;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务商管理类
 *
 * @author pangxiaoyu
 * @site
 * @date 2021-06-07 07:15
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/isvInfo")
public class IsvInfoController extends BaseController {

    private final ISysUserService userService;
    private final PayInterfaceConfigService payInterfaceConfigService;

    @Resource
    private SysOssPayMapper sysOssPayMapper;


    /**
     * 获取用户信息列表(用于绑定支付方式)
     */
    @GetMapping("/list")
    @SaCheckPermission("pay:isvInfo:list")
    public TableDataInfo<SysUser> list(SysUser user,PageQuery pageQuery) {
        return userService.selectPageUserList(user, pageQuery);
    }

   /**
    * <简述>查询服务商支付接口配置列表
    * <详细描述>
    * @author syf
    * @date 2025/1/6 17:07
    * @return com.ruoyi.alipay.domain.ApiRes<java.util.List < com.ruoyi.alipay.domain.PayInterfaceDefine>>
    */
    @GetMapping("/getPayList")
    @SaCheckPermission("pay:isvInfo:payList")
    public ApiRes<List<PayInterfaceDefine>> list(String  isvNo) {
        List<PayInterfaceDefine> list = payInterfaceConfigService.selectAllPayIfConfigListByIsvNo(CS.INFO_TYPE_ISV, isvNo);
        list.stream().map(this::matchingUrl).collect(Collectors.toList());
        return ApiRes.ok(list);
    }

    /**
     * 匹配Url
     * @return oss 匹配Url的OSS对象
     */
    private PayInterfaceDefine matchingUrl(PayInterfaceDefine en) {
        String ossId = en.getIcon();
        //根据ossId 获取 Oss
        SysOssPay sysOssPay = sysOssPayMapper.selectById(ossId);
        if (sysOssPay == null) return en;
        OssClient instance = OssFactory.instance(sysOssPay.getService());
        // 仅修改桶类型为 private 的URL，临时URL时长为120s
        if (AccessPolicyType.PRIVATE == instance.getAccessPolicy()) {
            en.setIcon(instance.getPrivateUrl(sysOssPay.getFileName(), 120));
        }
        return en;
    }

}
